//
//  GangCaiHeaderView.m
//  兼职页面
//
//  Created by kuiyuan liang on 2021/6/3.
//

#import "GangCaiHeaderView.h"
#import "CommonUtil.h"
#import "UUMarqueeView.h"
#import "GangCaiSelectAlert.h"
@interface GangCaiHeaderView()<UUMarqueeViewDelegate>

@property (strong, nonatomic) UIImageView * bgImageView;

@property (strong, nonatomic) UIImageView * iconImageView;
@property (strong, nonatomic) UILabel * label1;
@property (strong, nonatomic) UILabel * label2;

@property (strong, nonatomic) UIView * whiteView;
@property (strong, nonatomic) UILabel * ruleTitleLabel;
@property (strong, nonatomic) UUMarqueeView * adMarqueValue;
@property (strong, nonatomic) NSArray * adTitles;
@property (strong, nonatomic) UIView * marginView;
@property (strong, nonatomic) UIView * indexView;

@property (strong, nonatomic) UIView * tempView;
@property (strong, nonatomic) UIView * lineView;
@property (strong, nonatomic) UIView * tabView;
@property (strong, nonatomic) UIButton * lastSelect;
@property (strong, nonatomic) UIButton * lastTitleBtn;

@property (strong, nonatomic) UIView * lineView2;

@end

@implementation GangCaiHeaderView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        [self addSubview:self.bgImageView];
        [self addSubview:self.subcriBtn];
        [self addSubview:self.tongjiBtn];
        [self addSubview:self.tiaozhanBtn];
        [self addSubview:self.iconImageView];
        [self addSubview:self.label1];
        [self addSubview:self.label2];
        
        self.adTitles= @[@"澳彩名人堂的参赛规则是什么1,",
                                             @"澳彩名人堂的参赛规则是什么2;",
                                             @"澳彩名人堂的参赛规则是什么3"];
        
        [self.whiteView addSubview:self.ruleTitleLabel];
        [self.whiteView addSubview:self.adMarqueValue];
        [self.adMarqueValue reloadData];
        [self addSubview:self.marginView];
        
        [self addSubview:self.tabView];
        [self addSubview:self.whiteView];
        [self.tabView addSubview:self.lineView];
        [self.tabView addSubview:self.indexView];
        [self.tabView addSubview:self.tempView];
        
        NSArray *btns = @[@"特肖",@"特码",@"平特",@"半数",@"其他",@"杀榜"];
        CGFloat w  = SCREEN_WIDTH/btns.count;
        for (NSInteger i = 0; i<btns.count; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setTitle:btns[i] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [btn setTitleColor:RGB(255.0, 83.0, 133.0) forState:UIControlStateSelected];
            btn.titleLabel.font = [UIFont systemFontOfSize:AD(15)];
            [self.tabView addSubview:btn];
            [btn addTarget:self action:@selector(tabBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            btn.frame = CGRectMake(i * w, 0, w, AD(40));
            if (i == 0) {
                [self tabBtnClick:btn];
            }
        }
    }
    return self;
}
- (void)tabBtnClick:(UIButton *)sender{
    self.lastSelect.selected = false;
    self.lastSelect = sender;
    self.lastSelect.selected = YES;
    [UIView animateWithDuration:0.25 animations:^{
        self.indexView.centerX = sender.centerX;
    }];
    NSArray *titles;
    if ([sender.titleLabel.text isEqualToString:@"特肖"]) {
        titles = @[@"平特一肖",@"平特一尾",@"五不中",@"七不中",@"三中一",@"二连肖",@"二连尾"];
    }else if ([sender.titleLabel.text isEqualToString:@"特码"]){
        titles = @[@"平特一肖",@"平特一尾",@"五不中",@"七不中",@"三中一",@"二连肖"];
    }else if ([sender.titleLabel.text isEqualToString:@"平特"]){
        titles = @[@"平特一肖",@"平特一尾",@"五不中",@"七不中",@"三中一"];
    }else if ([sender.titleLabel.text isEqualToString:@"半数"]){
        titles = @[@"平特一肖",@"平特一尾",@"五不中",@"七不中"];
    }else if ([sender.titleLabel.text isEqualToString:@"其他"]){
        titles = @[@"平特一肖",@"平特一尾",@"五不中"];
    }else if ([sender.titleLabel.text isEqualToString:@"杀榜"]){
        titles = @[];
    }
    [self refreshBtnWithTiels:titles];
}

- (void)refreshBtnWithTiels:(NSArray *)titles {
    CGFloat x = AD(20);
    CGFloat y = AD(10);
    
    
    for (UIView *view in self.tempView.subviews) {
        [view removeFromSuperview];
    }
    
    [self.tempView addSubview:self.lineView2];
    [self.tempView addSubview:self.filterBtn];
    
    for (NSInteger i = 0; i < titles.count; i++) {
        NSString *title = titles[i];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:RGB(255.0, 83.0, 133.0) forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = [UIFont systemFontOfSize:AD(15)];
        btn.layer.cornerRadius = AD(12.5);
        btn.layer.borderWidth = AD(1);
        btn.layer.borderColor = RGB(243.0, 243.0, 243.0).CGColor;
        btn.backgroundColor = RGB(243.0, 243.0, 243.0);
        CGFloat w = [CommonUtil countWidth:title font:btn.titleLabel.font height:AD(35)] + AD(30);
        btn.frame = CGRectMake(x, y, w, AD(25));
        x = btn.right + AD(5);
        if (x > (SCREEN_WIDTH - AD(80))) {
            y = btn.bottom + AD(10);
            x = AD(20);
            btn.frame = CGRectMake(x, y, w, AD(25));
            x = btn.right + AD(5);
        }
        [self.tempView addSubview:btn];
        if (i == 0) {
            [self titleBtnClick:btn];
        }else if (i == (titles.count -1)){
            self.tempView.height = btn.bottom;
        }
    }
    if (titles.count == 0) {
        self.tempView.height = 0;
        self.tabView.height = self.tempView.bottom;
    }else{
        self.tabView.height = self.tempView.bottom + AD(10);
    }
    
    self.height = self.tabView.bottom;
    if (self.actionBlock) {
        self.actionBlock(self.lastSelect.titleLabel.text);
    }
    [self.tabView addSubview:self.lineView];
    [self.tabView addSubview:self.indexView];
    
}
- (void)titleBtnClick:(UIButton *)sender{
    self.lastTitleBtn.selected = false;
    self.lastTitleBtn.layer.borderColor = RGB(243.0, 243.0, 243.0).CGColor;
    self.lastTitleBtn.backgroundColor = RGB(243.0, 243.0, 243.0);
    self.lastTitleBtn = sender;
    self.lastTitleBtn.selected = YES;
    self.lastTitleBtn.layer.borderColor = RGB(255.0, 83.0, 133.0).CGColor;
    self.lastTitleBtn.backgroundColor = [UIColor whiteColor];
}



#pragma mark - UUMarqueeViewDelegate
- (NSUInteger)numberOfVisibleItemsForMarqueeView:(UUMarqueeView*)marqueeView {
    return 1;
}

- (NSUInteger)numberOfDataForMarqueeView:(UUMarqueeView*)marqueeView {
    return self.adTitles.count;
}
- (void)createItemView:(UIView*)itemView forMarqueeView:(UUMarqueeView*)marqueeView {

    itemView.backgroundColor = [UIColor whiteColor];
    UILabel *content = [[UILabel alloc] initWithFrame:itemView.bounds];
    content.font = [UIFont systemFontOfSize:AD(13)];
    content.tag = 1001;
    [itemView addSubview:content];
}
- (void)updateItemView:(UIView*)itemView atIndex:(NSUInteger)index forMarqueeView:(UUMarqueeView*)marqueeView {
    UILabel *content = [itemView viewWithTag:1001];
    content.text = self.adTitles[index];
    
}
    
- (CGFloat)itemViewHeightAtIndex:(NSUInteger)index forMarqueeView:(UUMarqueeView*)marqueeView {
    // for upwardDynamicHeightMarqueeView
    UILabel *content = [[UILabel alloc] init];
    content.numberOfLines = 0;
    content.font = [UIFont systemFontOfSize:AD(13)];
    content.text = [self.adTitles[index] objectForKey:@"content"];
    CGSize contentFitSize = [content sizeThatFits:CGSizeMake(CGRectGetWidth(marqueeView.frame) - 5.0f - 16.0f - 5.0f, MAXFLOAT)];
    return contentFitSize.height + 20.0f;
}

- (CGFloat)itemViewWidthAtIndex:(NSUInteger)index forMarqueeView:(UUMarqueeView*)marqueeView {
    // for leftwardMarqueeView
    UILabel *content = [[UILabel alloc] init];
    content.font = [UIFont systemFontOfSize:AD(13)];
    content.text = self.adTitles[index];
    return (5.0f + 16.0f + 5.0f) + content.intrinsicContentSize.width;  // icon width + label width (it's perfect to cache them all)
}

- (void)didTouchItemViewAtIndex:(NSUInteger)index forMarqueeView:(UUMarqueeView*)marqueeView {
    NSLog(@"Touch at index %lu - \"%@\"", (unsigned long)index, [self.adTitles[index] objectForKey:@"content"]);
}


#pragma mark -lazy
-(UIImageView *)bgImageView{
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"IMG_07391 2"]];
        _bgImageView.frame = CGRectMake(0, 0, SCREEN_WIDTH, AD(250));
        _bgImageView.userInteractionEnabled = YES;
    }
    return _bgImageView;
}

-(UIButton *)subcriBtn{
    if (!_subcriBtn) {
        _subcriBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_subcriBtn setTitle:@"我的订阅" forState:UIControlStateNormal];
        [_subcriBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _subcriBtn.titleLabel.font = [UIFont systemFontOfSize:AD(13)];
        _subcriBtn.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        _subcriBtn.layer.cornerRadius = AD(15);
        _subcriBtn.clipsToBounds = YES;
        _subcriBtn.frame = CGRectMake(AD(280), AD(10), AD(80), AD(30));
    }
    return _subcriBtn;
}

-(UIButton *)tongjiBtn{
    if (!_tongjiBtn) {
        _tongjiBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_tongjiBtn setTitle:@"统计" forState:UIControlStateNormal];
        [_tongjiBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _tongjiBtn.titleLabel.font = [UIFont boldSystemFontOfSize:AD(16)];
        _tongjiBtn.layer.cornerRadius = AD(30);
        _tongjiBtn.clipsToBounds = YES;
        _tongjiBtn.layer.borderColor = [UIColor whiteColor].CGColor;
        _tongjiBtn.layer.borderWidth = AD(2);
        _tongjiBtn.backgroundColor = RGB(0, 102.0, 247.0);
        _tongjiBtn.frame = CGRectMake(AD(300), _subcriBtn.bottom + AD(15), AD(60), AD(60));
    }
    return _tongjiBtn;
}

-(UIButton *)tiaozhanBtn{
    if (!_tiaozhanBtn) {
        _tiaozhanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_tiaozhanBtn setTitle:@"挑战" forState:UIControlStateNormal];
        [_tiaozhanBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _tiaozhanBtn.titleLabel.font = [UIFont boldSystemFontOfSize:AD(16)];
        _tiaozhanBtn.layer.cornerRadius = AD(30);
        _tiaozhanBtn.clipsToBounds = YES;
        _tiaozhanBtn.layer.borderColor = [UIColor whiteColor].CGColor;
        _tiaozhanBtn.layer.borderWidth = AD(2);
        _tiaozhanBtn.backgroundColor = RGB(237, 93, 111);
        _tiaozhanBtn.frame = CGRectMake(AD(300), _tongjiBtn.bottom + AD(10), AD(60), AD(60));
    }
    return _tiaozhanBtn;
}

-(UIImageView *)iconImageView{
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_gangcai_bei"]];
        _iconImageView.frame = CGRectMake(AD(15), _tiaozhanBtn.top+AD(15), AD(70), AD(70));
        _iconImageView.userInteractionEnabled = YES;
        _iconImageView.layer.cornerRadius = AD(30);
        _iconImageView.clipsToBounds = YES;
        _iconImageView.backgroundColor = [UIColor redColor];
    }
    return _iconImageView;
}

-(UILabel *)label1{
    if (!_label1) {
        _label1 = [[UILabel alloc] initWithFrame:CGRectMake(_iconImageView.right + AD(5), _iconImageView.top+AD(5), AD(100), AD(30))];
        _label1.text = @"名人堂";
        _label1.textColor = [UIColor whiteColor];
        _label1.font = [UIFont boldSystemFontOfSize:AD(22)];
    }
    return _label1;
}

-(UILabel *)label2{
    if (!_label2) {
        _label2 = [[UILabel alloc] initWithFrame:CGRectMake(_label1.right, _label1.bottom, AD(250), AD(30))];
        _label2.text = @"世界六合名人堂,终极荣耀目的地";
        _label2.textColor = [UIColor whiteColor];
        _label2.font = [UIFont boldSystemFontOfSize:AD(16)];
    }
    return _label2;
}

-(UIView *)whiteView{
    if (!_whiteView) {
        _whiteView = [[UIView alloc] initWithFrame:CGRectMake(AD(20), self.bgImageView.bottom - AD(40), self.width-AD(40), AD(50))];
        _whiteView.layer.cornerRadius = AD(10);
        _whiteView.backgroundColor = [UIColor whiteColor];
        _whiteView.layer.shadowColor = [UIColor blackColor].CGColor;
        _whiteView.layer.shadowOffset = CGSizeMake(0, 0 );
        _whiteView.layer.shadowOpacity = 0.3;
        _whiteView.layer.shadowRadius = AD(2);
    }
    return _whiteView;
}

-(UIView *)marginView{
    if (!_marginView) {
        _marginView = [[UIView alloc] initWithFrame:CGRectMake(0, self.bgImageView.bottom, SCREEN_WIDTH, AD(10))];
        _marginView.backgroundColor = [UIColor whiteColor];
    }
    return _marginView;
}

-(UILabel *)ruleTitleLabel{
    if (!_ruleTitleLabel) {
        _ruleTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(AD(20), 0, AD(40), self.whiteView.height)];
        _ruleTitleLabel.text = @"规则";
        _ruleTitleLabel.textColor = RGB(118.0, 117.0, 250.0);
        _ruleTitleLabel.font = [UIFont systemFontOfSize:AD(14)];
    }
    return _ruleTitleLabel;
}


-(UIView *)tabView{
    if (!_tabView) {
        _tabView = [[UIView alloc] initWithFrame:CGRectMake(0, _whiteView.bottom, SCREEN_WIDTH, AD(0))];
        _tabView.backgroundColor = [UIColor whiteColor];
    }
    return _tabView;
}


-(UUMarqueeView *)adMarqueValue{
    if (!_adMarqueValue) {
        _adMarqueValue = [[UUMarqueeView alloc] initWithFrame:CGRectMake(AD(60), 0,self.whiteView.width-AD(70) , self.whiteView.height)];
        _adMarqueValue.delegate = self;
        _adMarqueValue.timeIntervalPerScroll = 2.0f;
        _adMarqueValue.timeDurationPerScroll = 1.0f;

    }
    return _adMarqueValue;
}



-(UIView *)indexView{
    if (!_indexView) {
        _indexView = [[UIView alloc] initWithFrame:CGRectMake(0, _lineView.top, SCREEN_WIDTH/6, AD(1))];
        _indexView.backgroundColor = RGB(255.0, 83.0, 133.0);
    }
    return _indexView;
}

-(UIView *)lineView{
    if (!_lineView) {
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, AD(39), SCREEN_WIDTH, AD(1))];
        _lineView.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.3];
    }
    return _lineView;
}


-(UIView *)tempView{
    if (!_tempView) {
        _tempView = [[UIView alloc] initWithFrame:CGRectMake(0, AD(40), SCREEN_WIDTH, AD(0))];
        _tempView.backgroundColor = [UIColor whiteColor];
        _tempView.clipsToBounds = YES;
    }
    return _tempView;
}

-(UIView *)lineView2{
    if (!_lineView2) {
        _lineView2 = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-AD(80), AD(10), AD(1), AD(25))];
        _lineView2.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.3];
    }
    return _lineView2;
}

-(UIButton *)filterBtn{
    if (!_filterBtn) {
        _filterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_filterBtn setTitle:@"筛选" forState:UIControlStateNormal];
        [_filterBtn setImage:[UIImage imageNamed:@"icon_gangcai_filter"] forState:UIControlStateNormal];
        _filterBtn.frame = CGRectMake(_lineView2.right, AD(10), AD(100), AD(25));
        _filterBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -AD(55));
        _filterBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, AD(50));
        [_filterBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
       
    }
    return _filterBtn;
}


@end
